import { login, logout, getUserInfo } from '@/api/user'
import { setToken, getToken } from '@/libs/util'

export default {
  state: {
    userName: '',
    userId: '',
    avatorImgPath: '',
    token: getToken(),
    access: '',
    hasGetInfo: false,
    email: '',
    birthday: '',
    createdAt: '',
    no: '',
    phone: '',
    roleId: '',
    roleName: '',
    sex: ''
  },

  getters: {
    isLogin: state => {
      return (!!state.token)
    },
    getUserId: state => {
      let userIdLocal = window.localStorage.getItem('userId')
      if (state.userId && state.userId !== '') return state.userId
      if (state.userId === '' && userIdLocal) {
        return userIdLocal // 同步操作
      }
      return state.userId
    },
    getUserName: state => {
      let userNameLocal = window.localStorage.getItem('userName')
      if (state.userName && state.userName !== '') return state.userName
      if (state.userName === '' && userNameLocal) {
        return userNameLocal // 同步操作
      }
      return state.userName
    },
    getRoleName: state => {
      let roleNameLocal = window.localStorage.getItem('roleName')
      if (state.roleName && state.roleName !== '') return state.roleName
      if (state.roleName === '' && roleNameLocal) {
        return roleNameLocal // 同步操作
      }
      return state.roleName
    }
  },

  mutations: {
    // setAvator (state, avatorPath) {
    //   state.avatorImgPath = avatorPath
    // },
    setUserId (state, id) {
      state.userId = id
      window.localStorage.setItem('userId', id)
    },
    setUserName (state, name) {
      state.userName = name
      window.localStorage.setItem('userName', name)
    },
    setAccess (state, access) {
      state.access = access
    },
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    setHasGetInfo (state, status) {
      state.hasGetInfo = status
    },
    setEmail (state, email) {
      state.email = email
    },
    setSex (state, sex) {
      state.sex = sex
    },
    setRoleName (state, roleName) {
      state.roleName = roleName
      window.localStorage.setItem('roleName', roleName)
    },
    setRoleId (state, roleId) {
      state.roleId = roleId
    },
    setPhone (state, phone) {
      state.phone = phone
    },
    setNo (state, no) {
      state.no = no
    },
    setBirthday (state, birthday) {
      state.birthday = birthday
    }
  },
  actions: {
    // 登录
    handleLogin ({ commit }, { userName, password }) {
      userName = userName.trim()
      return new Promise((resolve, reject) => {
        login({
          userName,
          password
        }).then(res => {
          const data = res.data
          if (data.code === 200) {
            commit('setToken', data.records.token) // 设置token
          }
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 退出登录
    handleLogOut ({ state, commit }) {
      return new Promise((resolve, reject) => {
        // logout(state.token).then(() => {
        //   commit('setToken', '')
        //   commit('setAccess', [])
        //   resolve()
        // }).catch(err => {
        //   reject(err)
        // })
        // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
        commit('setToken', '')
        commit('setAccess', [])

        window.localStorage.removeItem('userName')
        window.localStorage.removeItem('ueserId')
        window.localStorage.removeItem('roleName')
        commit('setUserName', '')
        commit('setUserId', '')
        commit('setRoleName', '')
        resolve()
      })
    },
    // 获取用户相关信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          getUserInfo().then(res => {
            const data = res.data.records
            // commit('setAvator', data.avator)
            commit('setUserName', data.name)
            commit('setUserId', data.tid)
            commit('setAccess', data.access)
            commit('setSex', data.sex)
            commit('setEmail', data.email)
            commit('setRoleName', data.roleName)
            commit('setRoleId', data.roleId)
            commit('setPhone', data.phone)
            commit('setNo', data.no)
            commit('setBirthday', data.birthday)
            resolve(data)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    },

    // 判断是否登录
    isLogin: state => {
      return (!!state.token)
    }
  }
}
